package com.eight.cloud.admin.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eight.cloud.admin.entity.UserEntity;
import com.eight.cloud.admin.model.dto.UserDto;
import com.eight.cloud.admin.model.vo.UserDetail;
import com.eight.cloud.admin.service.IUserService;
import com.eight.cloud.core.global.model.LoginUser;
import com.eight.cloud.core.global.result.Result;
import com.eight.cloud.core.global.result.ResultCode;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.eight.cloud.core.global.constant.StrConstant.TENANT_ID;

/**
 * 用户 前端控制器
 *
 * @author TXC
 * @since 2024-10-02
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private IUserService userService;

    /**
     * 用户注册
     * @param userinfo 用户信息
     * @return 是否注册成功
     */
    @PostMapping("/register")
    public Result<String> register(@Validated @RequestBody UserDto userinfo) {
        UserEntity flag = userService.register(userinfo);
        if (ObjectUtil.isNull(flag)) {
            return Result.fail(ResultCode.ERROR);
        }
        return Result.ok();
    }

    /**
     * 添加用户
     * @param userinfo 用户信息
     * @return 是否注册成功
     */
    @PostMapping("/addUser")
    public Result<String> addUser(@Validated @RequestBody UserDto userinfo) {
        UserEntity flag = userService.addUser(userinfo);
        if (ObjectUtil.isNull(flag)) {
            return Result.fail(ResultCode.ERROR);
        }
        return Result.ok();
    }

    /**
     * 登陆接口时查询用户信息
     * @param username 用户名
     * @return 用户信息
     */
    @GetMapping("/userInfo")
    public Result<LoginUser> getUserinfo(String username) {
        HttpServletRequest request = ((ServletRequestAttributes)
                (RequestContextHolder.currentRequestAttributes())).getRequest();
        String tenantId = request.getHeader(TENANT_ID);
        LoginUser loginUser = userService.getUserDetails(username, tenantId);
        if (ObjectUtil.isNotNull(loginUser)) {
            return Result.ok(loginUser);
        }
        return Result.fail(ResultCode.USER_ACCOUNT_NOT_EXIST);
    }

    /**
     * 获取当前用户信息
     * @return 用户信息
     */
    @GetMapping("/userDetail")
    public Result<UserDetail> getUserDetails() {
        UserDetail userInfo = userService.getUserDetails();
        if (ObjectUtil.isNotNull(userInfo)) {
            return Result.ok(userInfo);
        }
        return Result.fail(ResultCode.USER_NOT_LOGIN);
    }

    /**
     * 用户列表
     * @return 用户列表
     */
    @GetMapping("/userList")
    public Result<IPage<UserEntity>> getUserPage(Page<UserEntity> page) {
        return Result.ok(userService.getUserList(page));
    }

    /**
     * 用户授权
     * @param id 用户id
     * @param roleIds 角色id列表
     * @return 是否授权成功
     */
    @PostMapping("/empower/{id}")
    public Result<String> empower(@PathVariable String id, @RequestBody List<String> roleIds) {
        boolean flag = userService.empower(id, roleIds);
        if (flag) {
            return Result.ok();
        }
        return Result.fail(ResultCode.ERROR);
    }

    /**
     * 编辑用户信息
     * @param id 用户id
     * @param userinfo 用户信息
     * @return 修改结果
     */
    @PostMapping("/edit/{id}")
    public Result<String> editUser(@PathVariable String id, @RequestBody UserDto userinfo) {
        boolean flag = userService.editUser(id, userinfo);
        if (flag) {
            return Result.ok();
        }
        return Result.fail(ResultCode.ERROR);
    }
}

